//package com.qst.crop.service.impl;
//
//import com.qst.crop.dao.FileUploaderDao;
//import com.qst.crop.entity.FileUploader;
//import com.qst.crop.service.FileUploaderService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.nio.file.Paths;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.UUID;
//import java.nio.file.Paths;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//@Service
//public class FileUploaderServiceImpl implements FileUploaderService {
//    @Autowired
//    private FileUploaderDao fileUploaderDao;
//
//    // slf4j 日志对象
//    private static final Logger logger = LoggerFactory.getLogger(FileUploaderServiceImpl.class);
//
//    @Override
//    public void uploadFile(String dev, String username, MultipartFile file) {
//        // 文件非空
//        if (!file.isEmpty()) {
//            // 获取文件名和文件类型
//            String originalFileName = file.getOriginalFilename();
//            String fileType = file.getContentType();
//            String extension = originalFileName.substring(originalFileName.lastIndexOf("."));
//
//            // 生成随机文件名
//            String randomFileName = UUID.randomUUID().toString().replace("-", "") + extension;
//
//            // 文件存储逻辑
//            String filePath = "D:\\file\\" + dev + "/" + randomFileName;
//            logger.info("——————————————impl file:{}", randomFileName);
//
//            // 查看是否存在目录，不存在则创建
//            File directory = new File(filePath).getParentFile();
//            if (!directory.exists()) {
//                directory.mkdirs();
//            }
//
//            try {
//                // 上传文件
//                file.transferTo(Paths.get(filePath));
//                // 文件上传成功才保存数据库
//                fileUploaderDao.updateAvatarName(username, randomFileName);
//                logger.info("File uploaded successfully: {}", randomFileName);
//            } catch (Exception e) {
//                logger.error("File upload failed for file: {}", randomFileName, e);
//            }
//
//            return randomFileName;
//        }
//        else{
//            return null;
//        }
//
//    }
//
//    @Override
//    public String getFileName(String username) {
//        return fileUploaderDao.getFileName(username);
//    }
//}
package com.qst.crop.service.impl;

import com.qst.crop.dao.FileUploaderDao;
import com.qst.crop.entity.FileUploader;
import com.qst.crop.service.FileUploaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
public class FileUploaderServiceImpl implements FileUploaderService {
    @Autowired
    private FileUploaderDao fileUploaderDao;

    /**
     * slf4j 日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(FileUploaderServiceImpl.class);

    @Override
    public void uploadFile(String dev,String username, MultipartFile file) {
        // 文件非空
        if (!file.isEmpty()) {

            // 获取文件名和文件类型
            String fileName = file.getOriginalFilename();
            String fileType = file.getContentType();

            // 文件存储逻辑（这里仅作示例，实际需要存储到服务器或云存储）
            String filePath = "D:\\file\\" + dev + "/" + fileName;
            logger.info("——————————————impl file:"+fileName);


            /*
                查看是否存在目录，不存在则创建
             */
            File directory = new File(filePath).getParentFile();
            if (!directory.exists()) {
                directory.mkdirs();
            }



            try {
                // 上传文件
                file.transferTo(Paths.get(filePath));
                // 文件上传成功才保存数据库
                fileUploaderDao.updateAvatarName(username,fileName);
                logger.info("File uploaded successfully: {}", fileName);
            }
            catch (Exception e){
                logger.error("File upload failed for file: {}", fileName,e);
            }
        }
    }

    @Override
    public String getFileName(String username){
        return fileUploaderDao.getFileName(username);
    }
}